package com.web.config;

import org.apache.ibatis.session.Configuration;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.context.annotation.Bean;

import javax.sql.DataSource;

/**
 * @author web
 * @version 1.0
 * @date 2023/12/27 22:28
 */
public class MybatisConfig {
    @Bean
    public SqlSessionFactoryBean sqlSessionFactoryBean(DataSource dataSource) {
        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
        sqlSessionFactoryBean.setTypeAliasesPackage("com.web.pojo");
        Configuration configuration = new Configuration();
        configuration.setLogImpl(org.apache.ibatis.logging.stdout.StdOutImpl.class);
        sqlSessionFactoryBean.setDataSource(dataSource);
        return sqlSessionFactoryBean;
    }

    @Bean
    public MapperScannerConfigurer mapperScannerConfigurer() {
        MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
        mapperScannerConfigurer.setBasePackage("com.web.mapper");
        return mapperScannerConfigurer;
    }

    /*用MapperFactoryBean 工厂创建mapper的bean对象*/
//    @Bean
//    public MapperFactoryBean<UserMapper> mapperFactoryBean(SqlSessionFactory sqlSessionFactory) {
//        MapperFactoryBean<UserMapper> userMapperMapperFactoryBean = new MapperFactoryBean<>(UserMapper.class);
//        userMapperMapperFactoryBean.setSqlSessionFactory(sqlSessionFactory);
//        return userMapperMapperFactoryBean;
//    }
}
